package com.jincheng.web.controller.system;

import com.jincheng.common.annotation.Log;
import com.jincheng.common.core.controller.BaseController;
import com.jincheng.common.core.domain.AjaxResult;
import com.jincheng.common.core.page.TableDataInfo;
import com.jincheng.common.enums.BusinessType;
import com.jincheng.common.utils.poi.ExcelUtil;
import com.jincheng.system.domain.Tribe;
import com.jincheng.system.domain.UserTribe;
import com.jincheng.system.domain.dto.TribeDetailDTO;
import com.jincheng.system.domain.dto.TribeScreenDTO;
import com.jincheng.system.domain.dto.TribeSimpleDTO;
import com.jincheng.system.service.ITribeService;
import com.jincheng.system.service.IUserTribeService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 部落Controller
 *
 * @author DairyJay
 * @date 2025-02-27
 */
@RestController
@RequestMapping("/system/tribe")
public class TribeController extends BaseController {

    private final ITribeService tribeService;
    private final IUserTribeService userTribeService;

    public TribeController(ITribeService tribeService, IUserTribeService userTribeService) {
        this.tribeService = tribeService;
        this.userTribeService = userTribeService;
    }

    /**
     * 查询部落列表
     */
    @PreAuthorize("@ss.hasPermi('system:tribe:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tribe tribe) {
        startPage();
        List<TribeDetailDTO> list = tribeService.selectTribeList(tribe);
        return getDataTable(list);
    }

    /**
     * 查询部落列表
     */
    @PreAuthorize("@ss.hasPermi('system:tribe:list')")
    @GetMapping("/list/options")
    public AjaxResult listOptions() {
        List<Map<String, Object>> list = tribeService.selectTribeListOptions();
        return success(list);
    }

    /**
     * 根据筛选条件查询活动列表
     *
     * @param tribeScreenDTO 筛选条件
     * @return 分页活动列表
     */
    @GetMapping("/listByScreen")
    public TableDataInfo listByScreen(TribeScreenDTO tribeScreenDTO) {
        startPage();
        List<Tribe> list = tribeService.selectTribeListByScreen(tribeScreenDTO);
        return getDataTable(list);
    }

    /**
     * 根据用户id查询部落列表
     */
    @GetMapping("/list/user/{status}")
    public TableDataInfo listByUser(@PathVariable("status") Integer status) {
        startPage();
        List<TribeSimpleDTO> list = tribeService.selectListByUser(status);
        return getDataTable(list);
    }

    /**
     * 获取部落统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult statistics() {
        return success(tribeService.getStatistics());
    }

    /**
     * 获取部落活动数统计信息
     */
    @GetMapping("/statistics/pie/{type}")
    public AjaxResult getPie(@PathVariable("type") String type) {
        return success(tribeService.getPie(type));
    }

    /**
     * 导出部落列表
     */
    @PreAuthorize("@ss.hasPermi('system:tribe:export')")
    @Log(title = "部落", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tribe tribe) {
        List<Tribe> list = tribeService.selectList(tribe);
        ExcelUtil<Tribe> util = new ExcelUtil<>(Tribe.class);
        util.exportExcel(response, list, "部落数据");
    }


    /**
     * 获取部落详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tribe:query')")
    @GetMapping(value = "/{tribeId}")
    public AjaxResult getInfo(@PathVariable("tribeId") Long tribeId) {
        return success(tribeService.selectTribeByTribeId(tribeId));
    }

    /**
     * 获取部落详细信息
     */
    @GetMapping("/detail/{tribeId}")
    public AjaxResult getDetail(@PathVariable("tribeId") Long tribeId) {
        TribeDetailDTO data = tribeService.selectTribeDetail(tribeId);
        AjaxResult ajax = AjaxResult.success(data);
        UserTribe userTribe = userTribeService.selectUserTribeByUserIdTribeId(tribeId);
        ajax.put("userStatus", userTribe == null ? -1 : userTribe.getStatus());
        return ajax;
    }

    /**
     * 新增部落
     */
    @PreAuthorize("@ss.hasPermi('system:tribe:add')")
    @Log(title = "部落", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tribe tribe) {
        return toAjax(tribeService.insertTribe(tribe));
    }

    /**
     * 修改部落
     */
    @PreAuthorize("@ss.hasPermi('system:tribe:edit')")
    @Log(title = "部落", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tribe tribe) {
        return toAjax(tribeService.updateTribe(tribe));
    }

    /**
     * 加入部落
     */
    @GetMapping("/apply/{id}")
    public AjaxResult apply(@PathVariable("id") Long tribeId) {
        return tribeService.apply(tribeId, getUserId());
    }

    /**
     * 删除部落
     */
    @PreAuthorize("@ss.hasPermi('system:tribe:remove')")
    @Log(title = "部落", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tribeIds}")
    public AjaxResult remove(@PathVariable Long[] tribeIds) {
        return toAjax(tribeService.deleteTribeByTribeIds(tribeIds));
    }
}
